
<div class="layui-card layadmin-header">
  <div class="layui-breadcrumb" lay-filter="breadcrumb">
    <a lay-href="">主页</a>
    <a><cite>订单管理</cite></a>
  </div>
</div>

<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-tab layui-tab-brief">
      <ul class="layui-tab-title">
        <li class="layui-this">退款订单</li>
      </ul>
      <div class="layui-tab-content">
        <div class="layui-tab-item layui-show">
          <div class="layui-row">
            <div class="layui-form" style="float:right; margin-bottom: 10px;">
              <div class="layui-form-item" style="margin:0;">
                <label class="layui-form-label">退款单号</label>
                <div class="layui-input-inline">
                  <input type="text" name="refundOrderId" id="refundOrderId" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">商户单号</label>
                <div class="layui-input-inline">
                  <input type="text" name="mchOrderNo" id="mchOrderNo" autocomplete="off" class="layui-input">
                </div>
                <label class="layui-form-label">状态</label>
                <div class="layui-input-inline">
                  <select name="type" id="state" lay-search="">
                    <option value="-99">所有状态</option>
                    <option value="0">订单生成</option>
                    <option value="1">支付中</option>
                    <option value="2">支付成功</option>
                    <option value="3">处理完成</option>
                  </select>
                </div>
                <button id="search" class="layui-btn" data-type="reload">
                  <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                </button>
              </div>
            </div>
          </div>
          <table id="Pay_Mgr_RefundOrder_dataAll" lay-filter="Pay_Mgr_RefundOrder_dataAll"></table>
        </div>

      </div>
    </div>
  </div>
</div>

<script type="text/html" id="payBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="detail">查看</a>
</script>

<script>
layui.use(['admin', 'table', 'util'], function(){
  var $ = layui.$
  ,admin = layui.admin
  ,table = layui.table
      ,view =layui.view
  ,element = layui.element
  ,form = layui.form;

  element.render('breadcrumb', 'breadcrumb');

  var tplState = function(d){
    if(d.state == 0) {
      return "<span style='color: blue'>订单生成</span>";
    }else if(d.state == 1) {
      return "<span style='color: orangered'>退款中</span>";
    }else if(d.state == 2) {
      return "<span style='color: green'>退款成功</span>";
    }else if(d.state == 3) {
      return "<span style='color: rebeccapurple'>退款失败</span>";
    }else if(d.state == 4) {
      return "<span style='color: darkgreen'>业务处理完成</span>";
    }
  };

  //用户列表
  table.render({
    elem: '#Pay_Mgr_RefundOrder_dataAll'
    ,url: layui.setter.baseUrl + '/refund_order/list' //用户列表接口
    ,where: {
        access_token: layui.data(layui.setter.tableName).access_token
    }
    ,id: 'tableReload'
    ,page: true
    ,cols: [[
      {type: 'checkbox', fixed: 'left'}
      ,{field: 'refundOrderId', title: '退款单号'}
      ,{field: 'mchId', title: '商户ID'}
      ,{field: 'refundAmount', title: '退款金额', templet: '<div>{{ d.refundAmount/100 }}</div>'}
      ,{field: 'state', title: '状态', templet: tplState}
      ,{field: 'createTime', title: '创建时间', templet: '<div>{{ layui.util.toDateString(d.createTime, "yyyy-MM-dd HH:mm:ss") }}</div>'}
      ,{field: 'edit', title: '操作', toolbar: '#payBar' }
    ]]
    ,skin: 'line'
  });

  //监听工具条
  table.on('tool(Pay_Mgr_RefundOrder_dataAll)', function(obj){
    var data = obj.data;
    if(obj.event === 'detail'){

        var  refundOrderId =data.refundOrderId;

        admin.req({
            type: 'get',
            url: layui.setter.baseUrl + '/refund_order/get',
            data: {
                refundOrderId : refundOrderId
            },
            error: function(err){
                layer.alert(JSON.stringify(err.field), {
                    title: '错误提示'
                })
            },
            success: function(res){
                if(res.code == 0){
                    data =res. data;
                    admin.popup({
                        title: '订单详情'
                        , area: ['710px', '700px']
                        , id: 'LAY-popup-content-view'
                        , success: function (layero, index) {
                            view(this.id).render('order/refund/view').done(function () {
                                form.render(null, 'layuiadmin-app-form-list');

                                var body =layer.getChildFrame('body',index);
                                body.find('#refundOrderId').val(data.refundOrderId);
                                form.val("layuiadmin-app-form-list",{

                                    "refundOrderId":res.data.refundOrderId,
                                    "mchOrderNo":data.mchOrderNo,
                                    "body":data.body,
                                    "errCode":data.errCode,
                                    'subject':data.subject,
                                    "amount":data.amount,
                                    "errMsg":data.errMsg,
                                    "state":data.state==0?"订单生成":data.state==1?"支付中":data.state==2?"支付成功":data.state==3?"处理完成":data.state==4?"已退款":"发生错误"


                                })
                                $('#mchId').val(data.mchId);
                                $('#appId').val(data.appId);
                                $('#channelId').val(data.channelId);
                                if(data.currency == "cny") {
                                    $('#currency').val("人民币");
                                }
                                $('#clientIp').val(res.data.clientIp);
                                $('#device').val(res.data.device);
                                $('#extra').val(res.data.extra);
                                $('#channelUser').val(res.data.channelUser);
                                $('#channelMchId').val(res.data.channelMchId);
                                $('#channelOrderNo').val(res.data.channelOrderNo);
                                $('#param1').val(res.data.param1);
                                $('#param2').val(res.data.param2);
                                $('#notifyUrl').val(res.data.notifyUrl);
                                if(res.data.expireTime != "" && res.data.expireTime != null) {
                                    $('#expireTime').val(layui.util.toDateString(res.data.expireTime));
                                }
                                if(res.data.paySuccTime != "" && res.data.paySuccTime != null) {
                                    $('#paySuccTime').val(layui.util.toDateString(res.data.paySuccTime));
                                }
                                if(res.data.createTime != "" && res.data.createTime != null) {
                                    $('#createTime').val(layui.util.toDateString(res.data.createTime));
                                }
                                if(res.data.updateTime != "" && res.data.updateTime != null) {
                                    $('#updateTime').val(layui.util.toDateString(res.data.updateTime));
                                }
                                form.render();

                            });
                        }
                    });
                }
            }
        })

    }
  });

  // 搜索
  var $ = layui.$, active = {
    reload: function(){
      var refundOrderId = $('#refundOrderId').val();
      var mchOrderNo = $("#mchOrderNo").val();
      var state = $("#state").val();
      //执行重载
      table.reload('tableReload', {
        page: {
          curr: 1 //重新从第 1 页开始
        }
        ,where: {
          refundOrderId: refundOrderId,
          mchOrderNo: mchOrderNo,
          state : state
        }
      });
    }
  };
  $('#search').on('click', function() {
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  // 渲染表单
  form.render();
});
</script>
